package com.sl.plat.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sl.common.ResponseData;
import com.sl.common.exception.GlobalErrorCodeEnum;
import com.sl.water.entity.RehearsalScheme;
import com.sl.water.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@RestController
@RequestMapping("/plat/inner/rehearsal")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预演-内部调用", description = "提供预演方案维护相关功能")
public class RehearsalInnerController {

    private final RehearsalSchemeService rehearsalSchemeService;

    private final BRCunService brCunService;

    private final BRDmService brDmService;

    private final BRYldService brYldService;

    private final BRCompanyService brCompanyService;

    private final BRInfoService brInfoService;

    @GetMapping("/{id}/{kind}")
    @Operation(summary = "根据ID获取预演方案参数")
    public ResponseData<JSONObject> get(@PathVariable long id, @PathVariable String kind) {
        JSONObject result = new JSONObject();
        RehearsalScheme rehearsalScheme = rehearsalSchemeService.getById(id);
        Integer simulationDuration = rehearsalScheme.getSimulationDuration();
        result.set("simulationDuration", simulationDuration);
        String params;
        if ("bfh".equals(kind)) {
            params = rehearsalScheme.getParamsBfh();
        } else {
            params = rehearsalScheme.getParamsFh();
        }
        result.set("params", JSONUtil.parseArray(params));
        LocalDateTime createdTime = rehearsalScheme.getCreatedTime();
        result.set("createTime", createdTime.toLocalDate());
        return ResponseData.success(result);
    }


    @PostMapping("/updateStatus/{id}")
    @Operation(summary = "修改状态")
    public ResponseData updateStatusById(@PathVariable long id, @RequestBody JSONObject data) {
        String error = "";
        if (data.containsKey("error") && ObjectUtils.isNotEmpty(data.getStr("error"))) {
            error = data.getStr("error");
        }
        rehearsalSchemeService.updateStatus(id, data.getInt("status"), error,data.getStr("kind"));
        return ResponseData.success("修改成功");
    }

    @PostMapping("/batchSaveCun")
    @Operation(summary = "批量保存存庄结果")
    public ResponseData batchSaveCun(@RequestBody JSONArray data) {
        if (brCunService.batchSave(data))
            return ResponseData.success("插入成功");
        else
            return ResponseData.error(GlobalErrorCodeEnum.INTERNAL_SERVER_ERROR, "插入失败");
    }

    @PostMapping("/batchSaveDm")
    @Operation(summary = "批量保存断面结果")
    public ResponseData batchSaveDm(@RequestBody JSONArray data) {
        if (brDmService.batchSave(data))
            return ResponseData.success("插入成功");
        else
            return ResponseData.error(GlobalErrorCodeEnum.INTERNAL_SERVER_ERROR, "插入失败");
    }

    @PostMapping("/batchSaveYld")
    @Operation(summary = "批量保存易涝点结果")
    public ResponseData batchSaveYld(@RequestBody JSONArray data) {
        if (brYldService.batchSave(data))
            return ResponseData.success("插入成功");
        else
            return ResponseData.error(GlobalErrorCodeEnum.INTERNAL_SERVER_ERROR, "插入失败");
    }

    @PostMapping("/batchSaveCompany")
    @Operation(summary = "批量保存企业厂区结果")
    public ResponseData batchSaveCompany(@RequestBody JSONArray data) {
        if (brCompanyService.batchSave(data))
            return ResponseData.success("插入成功");
        else
            return ResponseData.error(GlobalErrorCodeEnum.INTERNAL_SERVER_ERROR, "插入失败");
    }

    @PostMapping("/batchSaveInfo")
    @Operation(summary = "批量保存汇总结果结果")
    public ResponseData batchSaveInfo(@RequestBody JSONArray data) {
        if (brInfoService.batchSave(data))
            return ResponseData.success("插入成功");
        else
            return ResponseData.error(GlobalErrorCodeEnum.INTERNAL_SERVER_ERROR, "插入失败");
    }

}